/*
 * @Author: your name
 * @Date: 2022-04-12 19:14:42
 * @LastEditTime: 2022-04-12 22:57:00
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /safe_transmission_platform/src/codec.cc
 */
#include "codec.h"

namespace daniel
{
    // member function of Codec
    Codec::Codec()
    {
    }
    Codec::~Codec()
    {
    }
    // member function of Request
    RequestCodec::RequestCodec()
    {
    }
    //使用编码之后的protobuf进行初始化
    RequestCodec::RequestCodec(const std::string &encodeStr)
    {
        initMsg(encodeStr);
    } 
    //使用传入的变量构造一个RequestMsg对象
    RequestCodec::RequestCodec(const RequestInfo& reqInfo)
    {
        initMsg(reqInfo);
        return;
    }
    //通过编码之后的字符串，解析出msg的内容
    void RequestCodec::initMsg(const std::string &encodeStr)
    {
        m_encodeStr = encodeStr;
        return;
    }
    void RequestCodec::initMsg(const RequestInfo& reqInfo)
    {
        m_msg.set_cmdtype(reqInfo.cmd);
        m_msg.set_clientid(reqInfo.clientID);
        m_msg.set_serverid(reqInfo.serverID);
        m_msg.set_sign(reqInfo.sign);
        m_msg.set_data(reqInfo.data);
        return;
    }

    std::string RequestCodec::encodeMsg()
    {
        if (!m_encodeStr.empty()){
            return m_encodeStr;
        }
        std::string retStr;
        m_msg.SerializeToString(&retStr);
        return retStr;
    }
    //解码字符串，生成一个对象
    void *RequestCodec::decodeMsg()
    {
        RequestMsg *requestMsg = new RequestMsg;
        requestMsg->ParseFromString(this->m_encodeStr);
        return (void *)requestMsg;
    }
    RequestCodec::~RequestCodec()
    {
    }
    // member function of ResponseCodec
    ResponseCodec::ResponseCodec()
    {
    }
    ResponseCodec::ResponseCodec(const std::string& encodeStr)
    {
        initMsg(encodeStr);
    }
    ResponseCodec::ResponseCodec(const RespondInfo& respondInfo)
    {
        initMsg(respondInfo);
        return;
    }
    void ResponseCodec::initMsg(const std::string &encodeStr)
    {
        
        this->m_encodeStr = encodeStr;
    }
    void ResponseCodec::initMsg(const RespondInfo& respondInfo)
    {
        m_msg.set_rv(respondInfo.status);
        m_msg.set_seckeyid(respondInfo.seckeyID);
        m_msg.set_clientid(respondInfo.clientID);
        m_msg.set_serverid(respondInfo.serverID);
        m_msg.set_data(respondInfo.data);
        return;
    }
    std::string ResponseCodec::encodeMsg()
    {
        std::string retStr;
        m_msg.SerializeToString(&retStr);
        return retStr;
    }
    void *ResponseCodec::decodeMsg()
    {
        RespondMsg *respondMsg = new RespondMsg;
        respondMsg->ParseFromString(m_encodeStr);
        return (void *)respondMsg;
    }
    ResponseCodec::~ResponseCodec()
    {
    }
} // namespace daniel
